package cn.com.headfree.mouldshow.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.isc.User;
//import cn.com.headfree.base.controller.ResultData;
//import cn.com.headfree.callback.call.FileServerCall;
import cn.com.headfree.mouldshow.service.ExportWordService;
import cn.com.headfree.mouldshow.service.HumanDataService;
import cn.com.headfree.util.WordExportUtil;
import cn.com.headfree.vo.simple.EmpBaseInfoVo;
import com.alibaba.fastjson.JSON;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@CrossOrigin("*")
@RestController
@RequestMapping(value = "/exportWord",produces = "application/json;charset=UTF-8")
public class ExportWordController extends ExBaseController {
//    private static final Logger log=Logger.getLogger(ExportWordController.class);
    private static final Logger log = LogManager.getLogger(ExportWordController.class);

//    @Resource
//    private FileServerCall fileServerCall;

    //模板路径
    @Value("${pathOfTemplate}")
    private String pathOfTemplate;

    @Autowired
    private HumanDataService humanDataService;
    @Autowired
    private ExportWordService exportWordService;

    @Autowired
    ResourceLoader resourceLoader;

    /**
     * 按人员人资编号查询并导出个人信息到word
     * @param response
     * @param erpNo
     * @param filename
     */
    @GetMapping("/exportWord")
    public void exportWord(HttpServletResponse response,String erpNo,String filename){
//        ResultData fileList = fileServerCall.findFileList("ubiquitous_project", "5f3badff005449f2b780fed5a0d92b4d", "ub_declaration_file");
//        return fileList;
//        humanDataService.getFileList(response);

        try {
            if (StringUtils.isBlank(erpNo)||StringUtils.isBlank(filename)){
                log.error("人资管理导出人员个人信息异常:人资编号[erpNo]和文件名[fileName]不能为空!!!");
                return;
            }

            log.info("个人信息导出word,查询数据开始");
            //获取待导出的数据
            EmpBaseInfoVo modelData = humanDataService.getModelData(erpNo, pathOfTemplate);
            log.info("个人信息导出word,查询数据完成,开始导出word");

            //响应页面
            WordExportUtil.exportWord(response, modelData, pathOfTemplate, "empBaseInfo_segment.docx", filename,resourceLoader);
            log.info("个人信息导出word,导出word完成");
        }catch (Exception e){
            log.error("个人信息导出word异常!!!",e);
        }

    }

//    /**
//     * 导出人员信息word文档
//     * @param response
//     * @param erpNo
//     */
//    @GetMapping("/exportUserInfoWord")
//    public void exportUserInfoWord(HttpServletResponse response,String erpNo){
//        try {
//            if (StringUtils.isBlank(erpNo)){
//                log.error("导出人员信息word文档异常:人资编号[erpNo]和文件名[fileName]不能为空!!!");
//                return;
//            }
//            exportWordService.exportUserInfoWord(response,erpNo);
//        }catch (Exception e){
//            log.error("导出人员信息word文档异常!!!",e);
//        }
//
//    }

   
    /**
     * 导出人员信息word文档,根据模板
     * @param response
     * @param erpNo
     */
    @GetMapping("/exportUserInfoWord")
    public void exportUserInfoWordTpl(HttpServletResponse response,String erpNo){
        try {
            if (StringUtils.isBlank(erpNo)){
                log.error("导出人员信息word文档异常:人资编号[erpNo]和文件名[fileName]不能为空!!!");
                return;
            }
            exportWordService.exportUserInfoWordByTpl(response,erpNo, pathOfTemplate+"\resums.wps", "",resourceLoader);
        }catch (Exception e){
            log.error("导出人员信息word文档异常!!!",e);
        }
    }
}
